<extend name="Public/base"/>
<block name="body">
    <!-- 标题 -->
    <div class="main-title">
        <h2>
            云码列表页面
        </h2>
    </div>
    </div>
    <!-- 按钮工具栏 -->
    <div class="with-padding">
        <div class="fl">
            <button data-title="生成云码" modal-url="{:U('admin/Appstore/createCode')}" data-role="modal_popup" class="btn">
                生成云码
            </button>
            &nbsp;
            <button class="btn ajax-post btn" url="{:U('Admin/Appstore/setCodeStatus',array('status'=>1))}" target-form="ids">
                启用
            </button>
            &nbsp;
            <button class="btn ajax-post btn" url="{:U('Admin/Appstore/setCodeStatus',array('status'=>0))}" target-form="ids">
                禁用
            </button>
            &nbsp;
            <button class="btn ajax-post btn" url="{:U('Admin/Appstore/setCodeStatus',array('status'=>-1))}" target-form="ids">
                删除
            </button>
            &nbsp;
            <button class="btn" data-role="copy_code_list">批量复制云码</button>
            &nbsp;
            <button class="btn" data-role="cvs" data-url="{:U('Admin/Appstore/cvs')}">导出cvs</button>

            <!-- 选择框select -->
            <div style="float: right;">
                <style>
                    .oneselect {
                        display: inline-block;
                        margin-left: 10px;
                    }

                    .oneselect .title {
                        float: left;
                        line-height: 32px;
                    }

                    .oneselect .select_box {
                        float: left;
                        line-height: 32px;
                    }

                    .oneselect .select_box select {
                        min-width: 200px;
                    }
                </style>
                <form id="selectForm" method="get" action="{:U('Admin/Appstore/code')}" class="form-dont-clear-url-param">
                    <div class="oneselect">
                        <div class="title"></div>
                        <div class="select_box">
                            <select name="status" data-role="select_text" class="form-control">
                                <option value="-1" <if condition="$_GET['status'] eq -1">selected=""</if>>全部</option>
                                <option value="1" <if condition="$_GET['status'] eq 1">selected=""</if>>可用</option>
                                <option value="0" <if condition="$_GET['status'] eq 0">selected=""</if>>禁用</option>
                                <option value="2" <if condition="$_GET['status'] eq 2">selected=""</if>>已使用</option>
                            </select>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>


    <!-- 数据表格 -->
    <div class="with-padding">
        <table class="table table-bordered table-striped ">
            <!-- 表头 -->
            <thead>
            <tr>
                <th class="row-selected row-selected" style="width: 20px">
                    <input class="check-all" type="checkbox">
                </th>
                <th>ID</th>
                <th>云码</th>
                <th>标题</th>
                <th>创建者</th>
                <th>绑定产品</th>
                <th>创建时间</th>
                <th>当前状态</th>
                <th>操作</th>
            </tr>
            </thead>

            <!-- 列表 -->
            <tbody>
                <volist name="list" id="code">
                    <tr>
                        <td><input class="ids" type="checkbox" value="{$code.id}" name="ids[]" data-code="{$code.code}"></td>
                        <td>{$code.id}</td>
                        <td>{$code.code}</td>
                        <td>{$code.title}</td>
                        <td>
                            <a href="{:U('ucenter/index/index',array('uid'=>$code['uid']))}" target="_blank">{$code.user}</a>
                        </td>
                        <td>{$code.versions}</td>
                        <td>{$code.create_time|time_format}</td>
                        <td>
                            <eq name="code.status" value="1">
                                <a href="{:U('admin/appstore/setcodestatus',array('status'=>0,'ids'=>$code['id']))}" class="ajax-get">
                                    启用
                                </a>
                            </eq>
                            <eq name="code.status" value="0">
                                <a href="{:U('admin/appstore/setcodestatus',array('status'=>1,'ids'=>$code['id']))}" class="ajax-get">
                                    禁用
                                </a>
                            </eq>
                            <eq name="code.status" value="2">
                                已使用
                            </eq>
                        </td>
                        <td>
                            <div style="position: relative;">
                                <a data-role="copy_code" data-code="{$code.code}">复制云码</a>
                            </div>
                        </td>
                    </tr>
                </volist>
            </tbody>
        </table>
    </div>
    <!-- 分页 -->
    <div class="with-padding">
        {$pagination}
    </div>
    </div>
</block>
<block name="script">
    <script type="text/javascript" src="__STATIC__/thinkbox/jquery.thinkbox.js"></script>
    <script type="text/javascript" src="__PUBLIC__/js/ext/zclip/jquery.zclip.min.js"></script>
    <script type="text/javascript">
        $(document).on('submit', '.form-dont-clear-url-param', function (e) {
            e.preventDefault();

            var seperator = "&";
            var form = $(this).serialize();
            var action = $(this).attr('action');
            if (action == '') {
                action = location.href;
            }
            var new_location = action + seperator + form;

            location.href = new_location;

            return false;
        });
        $(function () {
            $('[data-role="copy_code"]').zclip({
                copy: function () {
                    return $(this).attr('data-code');
                },
                afterCopy: function () {
                    $(this).html('已复制');
                    toast.success('复制成功');
                }
            });
            $('[data-role="copy_code_list"]').zclip({
                copy: function () {
                    var code_list = '';
                    $('.ids').each(function () {
                        if ($(this).is(":checked")) {
                            code_list += $(this).attr('data-code') + '\n';
                        }
                    });
                    if (code_list != '') {
                        return code_list;
                    } else {
                        toast.error('请选择数据！');
                    }
                },
                afterCopy: function () {
                    toast.success('批量复制成功');
                }
            });
            $('[data-role="cvs"]').click(function () {
                var data_url = $(this).attr('data-url');
                var form = $('.ids');
                if (form.get(0) == undefined) {
                } else if (form.get(0).nodeName == 'FORM') {
                    query = form.serialize();
                } else if (form.get(0).nodeName == 'INPUT' || form.get(0).nodeName == 'SELECT' || form.get(0).nodeName == 'TEXTAREA') {
                    query = form.serialize();
                } else {
                    query = form.find('input,select,textarea').serialize();
                }
                data_url += '&' + query;
                window.open(data_url);
            });
            $('[data-role="select_text"]').change(function () {
                $('#selectForm').submit();
            });

            //模态弹窗
            $('[data-role="modal_popup"]').click(function(){
                var target_url=$(this).attr('modal-url');
                var data_title=$(this).attr('data-title');
                var target_form=$(this).attr('target-form');
                if(target_form!=undefined){
                    //设置了参数时，把参数加入
                    var form=$('.'+target_form);

                    if (form.get(0) == undefined) {
                        updateAlert('没有可操作数据。','danger');
                        return false;
                    } else if (form.get(0).nodeName == 'FORM') {
                        query = form.serialize();
                    } else if (form.get(0).nodeName == 'INPUT' || form.get(0).nodeName == 'SELECT' || form.get(0).nodeName == 'TEXTAREA') {
                        query = form.serialize();
                    } else {
                        query = form.find('input,select,textarea').serialize();
                    }
                    if(!query.length){
                        updateAlert('没有可操作数据。','danger');
                        return false;
                    }
                    target_url=target_url+'&'+query;
                }
                var myModalTrigger = new $.zui.ModalTrigger({
                    'type':'ajax',
                    'url':target_url,
                    'title':data_title
                });

                myModalTrigger.show();
            });
            //导航高亮
            highlight_subnav("{:U('Appstore/code')}");
        });
    </script>
</block>